package com.hyperz.svn;

import org.tmatesoft.svn.core.SVNAuthenticationException;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import android.os.AsyncTask;
import android.util.Log;

public class SVNConnect extends AsyncTask<Void, Void, SVNRepository> {
	
	private static final String LOG = "RunnableConnection";
	private RepositoryDetails mRepositoryDetails;
	private ProxyDetails mProxyDetails = null;
	
	public SVNConnect(RepositoryDetails repoDetails, ProxyDetails proxyDetails) {
		mRepositoryDetails = repoDetails;
		mProxyDetails = proxyDetails;
	}
	
	@Override
	protected SVNRepository doInBackground(Void... params) {
		SVNRepository svnRepo = null;
		
		try {
			try {
				svnRepo = getRepository(SVNURL.parseURIDecoded(mRepositoryDetails.getRepositoryURL()));
			} catch (SVNAuthenticationException authE) {
				Log.e(LOG, authE.getMessage());
			} 
		} catch (SVNException e) {
			Log.e(LOG, e.getMessage());
		}
		
		return svnRepo;
	}
	
	private SVNRepository getRepository(SVNURL url) throws SVNAuthenticationException {
		SVNRepository repository = null;

		try {
			repository = DAVRepositoryFactory.create(url); 
			repository.setAuthenticationManager(createAuthManager(mProxyDetails != null));
			repository.testConnection();
		} catch(SVNAuthenticationException authE) {
			throw authE;
		} catch(SVNException e) {
			Log.d(LOG, e.getMessage());
		}
		return repository;
	}
	
	private ISVNAuthenticationManager createAuthManager(boolean bUseProxy) {
		ISVNAuthenticationManager authManager;
		if (bUseProxy) {
			// create a basic auth manager passing the SVN user name and pwd to the constructor
			authManager = new BasicAuthenticationManager(mRepositoryDetails.getUserID(), mRepositoryDetails.getPassword());
			// then set the proxy information
			((BasicAuthenticationManager) authManager).setProxy(mProxyDetails.getProxyHost(), mProxyDetails.getProxyPort(), mProxyDetails.getProxyUserID(), mProxyDetails.getProxyPassword());
		} else {
			// no proxy required create a default auth manager
			authManager = SVNWCUtil.createDefaultAuthenticationManager(mRepositoryDetails.getUserID(), mRepositoryDetails.getPassword());
		}
		//return the auth manager
		return authManager;
	}
	
	public static class RepositoryDetails {
		private String mPath;
		private String mURL;
		private String mUID;
		private String mPWD;
		
		public RepositoryDetails() {
			this.mPath = "trunk";
		}
		
		public String getUserID() {
			return mUID;
		}

		public void setUserID(String userID) {
			this.mUID = userID;
		}

		public String getPassword() {
			return mPWD;
		}

		public void setPassword(String password) {
			this.mPWD = password;
		}
		
		public String getRepositoryPath() {
			return mPath;
		}
		
		public void setRepositoryPath(String path) {
			this.mPath = path;
		}
		
		public String getRepositoryURL() {
			return mURL;
		}
		
		public void setRepositoryURL(String url) {
			this.mURL = url;
		}
	}
	
	public static class ProxyDetails {
		private String mProxyHost;
		private int mProxyPort;
		private String mProxyUID;
		private String mProxyPWD;
		
		public String getProxyHost() {
			return mProxyHost;
		}
		
		public ProxyDetails() {
			//
		}
		
		public void setProxyHost(String proxyHost) {
			this.mProxyHost = proxyHost;
		}
		
		public int getProxyPort() {
			return mProxyPort;
		}
		
		public void setProxyPort(String proxyPort) {
			this.mProxyPort = Integer.parseInt(proxyPort);
		}
		
		public void setProxyPort(int proxyPort) {
			this.mProxyPort = proxyPort;
		}
		
		public String getProxyUserID() {
			return mProxyUID;
		}
		
		public void setProxyUserID(String proxyUID) {
			this.mProxyUID = proxyUID;
		}
		
		public String getProxyPassword() {
			return mProxyPWD;
		}
		
		public void setProxyPassword(String proxyPWD) {
			this.mProxyPWD = proxyPWD;
		}
	}


	
}
